﻿using Dapper;
using HrEFCore;
using HrEFCore.Models;
using HrIService;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HrService
{
	
	public class RolesService:IRolesService
	{
		private readonly MyDbContext myDbContext;

		public RolesService(MyDbContext myDbContext)
        {
			this.myDbContext = myDbContext;
		}

		public async Task<int> AddRoles(Roles roles)//添加
		{
			myDbContext.Roles.Add(roles);
			return await myDbContext.SaveChangesAsync();
		}

		public async Task<int> DelRoles(short id)//删除
		{
			Roles roles = await myDbContext.Roles.FirstOrDefaultAsync(e=>e.RolesID == id);
			myDbContext.Roles.Remove(roles);
			return await myDbContext.SaveChangesAsync();
		}

		public Task<Roles> SelectById(short id)//根据id查询
		{
			return myDbContext.Roles.FirstOrDefaultAsync(e=>e.RolesID==id);
			
		}
		public async Task<FenYe2<Roles>> SelectRolesByFY(FenYe2<Roles> fenYe)//分页
		{
			using (var conn = myDbContext.Database.GetDbConnection())
			{
				DynamicParameters dp = new DynamicParameters();
				dp.Add("@currentPage", fenYe.CurrentPage);
				dp.Add("@pageSize", fenYe.PageSize);
				dp.Add("@keyName", "RolesID");
				dp.Add("@tableName", "Roles");
				dp.Add("@rows", direction: ParameterDirection.Output, dbType: DbType.Int32);
				string sql = "exec [dbo].[proc_FenYe] @currentPage,@pageSize,@keyName,@tableName,@rows out";
				fenYe.List = await conn.QueryAsync<Roles>(sql, dp);
				fenYe.Rows = dp.Get<int>("rows");
				return fenYe;
			}
		}
		public Task<int> UpdRoles(Roles roles)//修改
		{
			myDbContext.Roles.Update(roles);
			return myDbContext.SaveChangesAsync();
		}
		public List<Menu> SelectMenuBy()
		{
			using (var conn = myDbContext.Database.GetDbConnection())
			{
				string sql = $"select j.JuriID,j.JurName,j.GroupID,j.JurAddress " +
					$"from [dbo].[Jurisdiction] j ";
				List<Jurisdiction> jurisdictions = conn.Query<Jurisdiction>(sql).ToList();
				List<Menu> menus = new List<Menu>();
				//筛选
				foreach (Jurisdiction item in jurisdictions.Where(e => e.GroupID == 0))
				{
					Menu menu = new Menu()
					{
						JuriID = item.JuriID,
						JurName = item.JurName,
						GroupID = item.GroupID,
						JurAddress = item.JurAddress
					};
					menu.Children = GetChild(jurisdictions, item.JuriID);
					menus.Add(menu);
				}
				return menus;
			}
		}
		public List<Menu> SelectMenuById(short id)
		{
			using (var conn = myDbContext.Database.GetDbConnection())
			{
				string sql = $"select j.JuriID,j.JurName,j.GroupID,j.JurAddress " +
					$"from [dbo].[Jurisdiction] j " +
					$"inner join [dbo].[RolesJurisdiction] rj on j.JuriID= rj.JuriID " +
					$"where rj.RolesID={id}";
				List<Jurisdiction> jurisdictions = conn.Query<Jurisdiction>(sql).ToList();
				List<Menu> menus = new List<Menu>();
				//筛选
				foreach (Jurisdiction item in jurisdictions.Where(e => e.GroupID == 0))
				{
					Menu menu = new Menu()
					{
						JuriID = item.JuriID,
						JurName = item.JurName,
						GroupID = item.GroupID,
						JurAddress = item.JurAddress
					};
					menu.Children = GetChild(jurisdictions, item.JuriID);
					menus.Add(menu);
				}
				return menus;
			}
		}

		private List<Menu> GetChild(List<Jurisdiction> jurisdictions, short juriID)
		{
			List<Menu> menus = new List<Menu>();
			var children = jurisdictions.Where(e => e.GroupID == juriID);
			if (children.Any())
			{
				foreach (Jurisdiction item in children)
				{
					Menu menu = new Menu()
					{
						JuriID = item.JuriID,
						JurName = item.JurName,
						GroupID = item.GroupID,
						JurAddress = item.JurAddress,
					};
					menu.Children = GetChild2(jurisdictions, item.JuriID);
					menus.Add(menu);
				}
			}
			return menus;
		}

		private List<Menu> GetChild2(IEnumerable<Jurisdiction> jurisdictions, short juriID)
		{
			List<Menu> menus = new List<Menu>();
			var children = jurisdictions.Where(e => e.GroupID == juriID);
				foreach (Jurisdiction item in children)
				{
					Menu menu = new Menu()
					{
						JuriID = item.JuriID,
						JurName = item.JurName,
						GroupID = item.GroupID,
						JurAddress = item.JurAddress,
					};
					menu.Children = GetChild2(jurisdictions, item.JuriID);
					menus.Add(menu);
				}
			return menus;
		}

		public int UpdateQuanXian(RolesJuri r)
		{
			using(var conn=myDbContext.Database.GetDbConnection())
			{
				var param = new DynamicParameters();
				param.Add("RolesID", dbType:DbType.Int16,direction:ParameterDirection.Input,value:r.RolesID);
				param.Add("JuriIDs", dbType: DbType.String, direction: ParameterDirection.Input, value: r.JuriID);
				string sql = "quanxian";
				int row = conn.Execute(sql,param,commandType:CommandType.StoredProcedure); 
				if(row > 0)
				{
					return row;
				}
				else
				{
					return 0;
				}
			}
		}

		public int GetRolesID(int UId)
		{
			var userRoles = myDbContext.UserRoles.FirstOrDefault(e => e.UserID == UId);
			if (userRoles != null)
			{
				return userRoles.RolesID;
			}
			else
			{
				throw new ArgumentException("该用户不存在对应的角色");
			}
		}
	}
}
